﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace msBsc.SvRef
{
    public class CEdiString
    {
        public static string CutString(string inputString, int len)
        {
            ASCIIEncoding ascii = new ASCIIEncoding();
            int tempLen = 0;
            string tempString = "";
            byte[] s = ascii.GetBytes(inputString);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                {
                    tempLen += 2;
                }
                else
                {
                    tempLen += 1;
                }

                try
                {
                    tempString += inputString.Substring(i, 1);
                }
                catch
                {
                    break;
                }

                if (tempLen >= len)
                    break;
            }
            //如果截过则加上半个省略号 
            //byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
            //if (mybyte.Length > len)
            //    tempString += "…";
            for (int i = tempLen + 1; i <= len; i++) tempString += " ";

            return tempString;
        }
        public static string CutString(string inputString, int len, bool prevSpace)
        {
            ASCIIEncoding ascii = new ASCIIEncoding();
            int tempLen = 0;
            string tempString = "";
            byte[] s = ascii.GetBytes(inputString);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                {
                    tempLen += 2;
                }
                else
                {
                    tempLen += 1;
                }

                try
                {
                    tempString += inputString.Substring(i, 1);
                }
                catch
                {
                    break;
                }

                if (tempLen >= len)
                    break;
            }
            //如果截过则加上半个省略号 
            //byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
            //if (mybyte.Length > len)
            //    tempString += "…";
            for (int i = tempLen + 1; i <= len; i++) tempString = prevSpace ? " " + tempString : tempString + " ";

            return tempString;
        }

        public static string CutString(string inputString, int len, bool prevSpace, string strSp)
        {
            ASCIIEncoding ascii = new ASCIIEncoding();
            int tempLen = 0;
            string tempString = "";
            byte[] s = ascii.GetBytes(inputString);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                {
                    tempLen += 2;
                }
                else
                {
                    tempLen += 1;
                }

                try
                {
                    tempString += inputString.Substring(i, 1);
                }
                catch
                {
                    break;
                }

                if (tempLen >= len)
                    break;
            }
            //如果截过则加上半个省略号 
            //byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
            //if (mybyte.Length > len)
            //    tempString += "…";
            for (int i = tempLen + 1; i <= len; i++) tempString = prevSpace ? strSp + tempString : tempString + strSp;

            return tempString;
        }

        public static string EdiCode(DataTable dt, string minshengCode)
        {
            if (dt == null) return minshengCode;
            dt.DefaultView.RowFilter = "EDI_CODE_B='" + minshengCode + "'";
            if (dt.DefaultView.Count > 0) return dt.DefaultView[0]["EDI_CODE_A"].ToString();
            else return minshengCode;
        }
    }

    public class CITDecimal
    {
        /// <summary> 
        /// 保留两位小数，四舍五入 
        /// </summary> 
        /// <param name="dValue"></param> 
        /// <returns></returns> 
        public static decimal ToRoundDecimal2(decimal dValue)
        {
            decimal returnValue = dValue;
            decimal tmpValue = 0.5M;
            if (dValue > 0)
            {
                tmpValue = (decimal)((int)(dValue * 100 + tmpValue));
                returnValue = tmpValue / 100;
            }
            else
            {
                tmpValue = (decimal)((int)(dValue * 100 - tmpValue));
                returnValue = tmpValue / 100;
            }
            return returnValue;
        }

        /// <summary> 
        /// 四舍五入 ,保留位数由用户自定
        /// </summary> 
        /// <param name="dValue"></param> 
        /// <returns></returns> 
        public static decimal ToRoundDecimal(decimal dValue, int digits)
        {
            if (dValue == 0) return dValue;
            decimal returnValue = dValue;
            decimal tmpValue = 0.5M;
            int iDigits = 1;
            for (int i = 0; i < digits; i++)
            {
                iDigits = iDigits * 10;
            }

            if (dValue > 0)
            {
                tmpValue = (decimal)((int)(dValue * iDigits + tmpValue));
                returnValue = (digits == 0) ? tmpValue : tmpValue / iDigits;
            }
            else
            {
                tmpValue = (decimal)((int)(dValue * iDigits - tmpValue));
                returnValue = (digits == 0) ? tmpValue : tmpValue / iDigits;
            }
            return returnValue;
        }

        public static decimal Money(DataRow row, string strField)
        {
            return CITContainer.Weight(row, strField);
        }
        public static decimal Money(DataRowView row, string strField)
        {
            return CITContainer.Weight(row, strField);
        }
        public static decimal Money(DataRow row, string strField, int digits)
        {
            return CITContainer.Weight(row, strField);
        }
        public static decimal Money(DataRowView row, string strField, int digits)
        {
            return CITContainer.Weight(row, strField);
        }
    }

    public class CITContainer
    {

        #region CheckContainerNo 效验集装箱号码
        /// <summary>
        /// 效验集装箱号码
        /// </summary>
        /// <param name="containerNo">集装箱号码</param>
        /// <returns>
        /// 箱号合法，返回true；箱号不合法，返回false
        /// </returns>
        public static bool CheckContainerNo(string strContainerNo)
        {
            if (strContainerNo == null || strContainerNo == "") return true;
            if (strContainerNo.Trim().Length != 11) return false;//箱号长度应等于11
            //箱号对应的字符数组
            char[] chr = strContainerNo.ToCharArray();
            //标准串
            string[] standardStr = new string[] { "10010203040506070809", "09020406081001030507", "07040801050902061003", "03080502100704010906" };
            string[] ctnStr = new string[4];
            for (int i = 0; i < 4; i++)
            {
                if (chr[i] > 'Z' || chr[i] < 'A')
                    return false;
                ctnStr[i] = standardStr[i].Substring((((chr[i] - 'A') % 10 + 1) * 2 - 2), 2);
            }
            for (int j = 4; j < 11; j++)
            {
                if (chr[j] > '9' || chr[j] < '0')
                    return false;
            }
            int r1 = 0, r2, r3;
            foreach (string str in ctnStr)
            {
                r1 += Convert.ToInt32(str);
            }
            r1 = r1 % 11;
            r2 = ((Convert.ToInt32(chr[4].ToString()) * 5) + (Convert.ToInt32(chr[5].ToString()) * 10) +
                (Convert.ToInt32(chr[6].ToString()) * 9) + (Convert.ToInt32(chr[7].ToString()) * 7) +
                (Convert.ToInt32(chr[8].ToString()) * 3) + (Convert.ToInt32(chr[9].ToString()) * 6)) % 11;
            r3 = (r1 + r2) % 11;
            if (r3 == 10)
                r3 = 0;
            if (r3 == Convert.ToInt32(chr[10].ToString()))
                return true;
            else
                return false;
        }


        #endregion

        public static int TEU(DataRow row, string strField20, string strField40, string strFieldHQ)
        {
            if (row == null) return 0;
            int i20, i40, iHQ;

            i20 = i40 = iHQ = 0;

            if (row.Table.Columns.IndexOf(strField20) > -1)
            {
                if (String.IsNullOrEmpty(row[strField20].ToString()) == false)
                {
                    try
                    {
                        i20 = Int32.Parse(row[strField20].ToString());
                    }
                    catch
                    {
                        i20 = 0;
                    }
                }
            }
            ///////////////////
            if (row.Table.Columns.IndexOf(strField40) > -1)
            {
                if (String.IsNullOrEmpty(row[strField40].ToString()) == false)
                {
                    try
                    {
                        i40 = Int32.Parse(row[strField40].ToString());
                    }
                    catch
                    {
                        i40 = 0;
                    }
                }
            }
            ///////////////////////
            if (row.Table.Columns.IndexOf(strFieldHQ) > -1)
            {
                if (String.IsNullOrEmpty(row[strFieldHQ].ToString()) == false)
                {
                    try
                    {
                        iHQ = Int32.Parse(row[strFieldHQ].ToString());
                    }
                    catch
                    {
                        iHQ = 0;
                    }
                }
            }

            /////////////...............
            /////////////////////////////////
            return i20 + 2 * i40 + 2 * iHQ;
        }
        public static int TEU(DataRowView row, string strField20, string strField40, string strFieldHQ)
        {
            if (row == null) return 0;
            int i20, i40, iHQ;

            i20 = i40 = iHQ = 0;

            if (row.DataView.Table.Columns.IndexOf(strField20) > -1)
            {
                if (String.IsNullOrEmpty(row[strField20].ToString()) == false)
                {
                    try
                    {
                        i20 = Int32.Parse(row[strField20].ToString());
                    }
                    catch
                    {
                        i20 = 0;
                    }
                }
            }
            ///////////////////
            if (row.DataView.Table.Columns.IndexOf(strField40) > -1)
            {
                if (String.IsNullOrEmpty(row[strField40].ToString()) == false)
                {
                    try
                    {
                        i40 = Int32.Parse(row[strField40].ToString());
                    }
                    catch
                    {
                        i40 = 0;
                    }
                }
            }
            ///////////////////////
            if (row.DataView.Table.Columns.IndexOf(strFieldHQ) > -1)
            {
                if (String.IsNullOrEmpty(row[strFieldHQ].ToString()) == false)
                {
                    try
                    {
                        iHQ = Int32.Parse(row[strFieldHQ].ToString());
                    }
                    catch
                    {
                        iHQ = 0;
                    }
                }
            }

            /////////////...............
            /////////////////////////////////
            return i20 + 2 * i40 + 2 * iHQ;
        }

        public static int TEU(DataRow row, string strQtyField, string strTypeField, ref int iC20, ref int iC40, ref int iCHQ)
        {
            if (row == null) return 0;
            iC20 = iC40 = iCHQ = 0;
            int tQty = 0;

            if (row.Table.Columns.IndexOf(strQtyField) > -1 && row.Table.Columns.IndexOf(strTypeField) > -1)
            {
                tQty = Qty(row, strQtyField);
                if (String.IsNullOrEmpty(row[strTypeField].ToString())) iC20 = tQty;
                else
                {
                    if (row[strTypeField].ToString().Substring(0, 1) == "1") iC20 = tQty;
                    if (row[strTypeField].ToString().Substring(0, 1) == "2") iC20 = tQty;
                    if (row[strTypeField].ToString().Substring(0, 1) == "4")
                    {
                        if (row[strTypeField].ToString().Substring(0, 4) == "40HC") iCHQ = tQty;
                        else if (row[strTypeField].ToString().Substring(0, 4) == "40HQ") iCHQ = tQty;
                        else iC40 = tQty;
                    }
                }
                return iC20 + 2 * iC40 + 2 * iCHQ;

            } return 0;

        }

        public static int TEU(DataRowView row, string strQtyField, string strTypeField, ref int iC20, ref int iC40, ref int iCHQ)
        {
            if (row == null) return 0;
            iC20 = iC40 = iCHQ = 0;
            int tQty = 0;

            if (row.DataView.Table.Columns.IndexOf(strQtyField) > -1 && row.DataView.Table.Columns.IndexOf(strTypeField) > -1)
            {
                tQty = Qty(row, strQtyField);
                if (String.IsNullOrEmpty(row[strTypeField].ToString())) iC20 = tQty;
                else
                {
                    if (row[strTypeField].ToString().Substring(0, 1) == "1") iC20 = tQty;
                    if (row[strTypeField].ToString().Substring(0, 1) == "2") iC20 = tQty;
                    if (row[strTypeField].ToString().Substring(0, 1) == "4")
                    {
                        if (row[strTypeField].ToString().Substring(0, 4) == "40HC") iCHQ = tQty;
                        else if (row[strTypeField].ToString().Substring(0, 4) == "40HQ") iCHQ = tQty;
                        else iC40 = tQty;
                    }
                }
                return iC20 + 2 * iC40 + 2 * iCHQ;

            } return 0;

        }


        public static int Qty(DataRowView row, string strField)
        {
            if (row == null) return 0;
            int i20 = 0;

            if (row.DataView.Table.Columns.IndexOf(strField) > -1)
            {
                if (String.IsNullOrEmpty(row[strField].ToString()) == false)
                {
                    try
                    {
                        i20 = Int32.Parse(row[strField].ToString());
                    }
                    catch
                    {
                        i20 = 0;
                    }
                }
            }
            return i20;
        }
        public static int Qty(DataRow row, string strField)
        {
            if (row == null) return 0;
            int i20 = 0;

            if (row.Table.Columns.IndexOf(strField) > -1)
            {
                if (String.IsNullOrEmpty(row[strField].ToString()) == false)
                {
                    try
                    {
                        i20 = Int32.Parse(row[strField].ToString());
                    }
                    catch
                    {
                        i20 = 0;
                    }
                }
            }
            return i20;
        }

        public static decimal Weight(DataRow row, string strField)
        {
            if (row == null) return 0;
            decimal dWeight = 0;

            if (row.Table.Columns.IndexOf(strField) > -1)
            {
                if (String.IsNullOrEmpty(row[strField].ToString()) == false)
                {
                    try
                    {
                        dWeight = decimal.Parse(row[strField].ToString());
                    }
                    catch
                    {
                        dWeight = 0;
                    }
                }
            }
            return dWeight;
        }
        public static decimal Weight(DataRow row, string strField, int digits)
        {
            if (row == null) return 0;
            decimal dWeight = 0;

            if (row.Table.Columns.IndexOf(strField) > -1)
            {
                if (String.IsNullOrEmpty(row[strField].ToString()) == false)
                {
                    try
                    {
                        dWeight = decimal.Parse(row[strField].ToString());
                        dWeight = CITDecimal.ToRoundDecimal(dWeight, digits);
                    }
                    catch
                    {
                        dWeight = 0;
                    }
                }
            }
            return dWeight;
        }
        public static decimal Weight(DataRowView row, string strField)
        {
            if (row == null) return 0;
            decimal dWeight = 0;

            if (row.DataView.Table.Columns.IndexOf(strField) > -1)
            {
                if (String.IsNullOrEmpty(row[strField].ToString()) == false)
                {
                    try
                    {
                        dWeight = decimal.Parse(row[strField].ToString());
                    }
                    catch
                    {
                        dWeight = 0;
                    }
                }
            }
            return dWeight;
        }
        public static decimal Weight(DataRowView row, string strField, int digits)
        {
            if (row == null) return 0;
            decimal dWeight = 0;

            if (row.DataView.Table.Columns.IndexOf(strField) > -1)
            {
                if (String.IsNullOrEmpty(row[strField].ToString()) == false)
                {
                    try
                    {
                        dWeight = decimal.Parse(row[strField].ToString());
                        dWeight = CITDecimal.ToRoundDecimal(dWeight, digits);
                    }
                    catch
                    {
                        dWeight = 0;
                    }
                }
            }
            return dWeight;
        }
        public static decimal Cube(DataRow row, string strField, int digits)
        {
            return Weight(row, strField, digits);
        }
        public static decimal Cube(DataRow row, string strField)
        {
            return Weight(row, strField);
        }
        public static decimal Cube(DataRowView row, string strField)
        {
            return Weight(row, strField);
        }
        public static decimal Cube(DataRowView row, string strField, int digits)
        {
            return Weight(row, strField, digits);
        }

        public static decimal Money(DataRow row, string strField)
        {
            return Weight(row, strField);
        }
        public static decimal Money(DataRowView row, string strField)
        {
            return Weight(row, strField);
        }
        public static decimal Money(DataRow row, string strField, int digits)
        {
            return Weight(row, strField);
        }
        public static decimal Money(DataRowView row, string strField, int digits)
        {
            return Weight(row, strField);
        }
    }

}
